#ifndef _DSWFIFO_BASE_H
#define _DSWFIFO_BASE_H

#include <libbase/dswbase.h>
#include <fcntl.h>
#include <termios.h>

#define MAX_FIFO_BUFFER_SIZE (5 * 1024)

typedef struct{
	char nData[MAX_FIFO_BUFFER_SIZE];
	char* pPos;
} ST_FIFO_RECV_T, *LPST_FIFO_RECV_T;

class CDswfifo_base : public CDswbase
{
public:
	CDswfifo_base(LPST_ENV_T pEnv, const char* pFifo_R, const char* pFifo_W);
	virtual ~CDswfifo_base();
	bool IsValid(){return ((m_nFifo_R != -1) && (m_nFifo_W != -1));};
private:
	virtual bool SeekToStartComm(LPST_FIFO_RECV_T pFifoRecv, int &nPayloadSize);
	virtual void ProcessData(LPST_FIFO_RECV_T pFifoRecv){};
	virtual bool CheckValidData(LPST_FIFO_RECV_T pFifoRecv){return false;};
protected:
	virtual void OnCreateFIFO(){};
	virtual void ResetBuffer();
private:
	bool OpenFifo(const char* pFifo_R, const char* pFifo_W);

	static void cb_OnRead(struct bufferevent *bev, void *ctx){((CDswfifo_base*)ctx)->OnRead(bev);};
	virtual void OnRead(struct bufferevent *bev);
	static void cb_OnEvent(struct bufferevent *bev, short events, void *ctx){((CDswfifo_base*)ctx)->OnEvent(bev, events);};
	virtual void OnEvent(struct bufferevent *bev, short events){};

	static void cbCreateFIFOTask(int fs, short events, void* ctx){((CDswfifo_base*)ctx)->CreateFIFOTask();};
	void CreateFIFOTask();
protected:
	int m_nFifo_R, m_nFifo_W;
	struct bufferevent *m_pBev_R, *m_pBev_W;
	int m_nWantSize;
	bool m_bStartComm;
	ST_FIFO_RECV_T m_nFifoRecv;
	ST_TIMER_PARAM_T m_nCreateFIFO_Task;
	const char *m_pFifo_R, *m_pFifo_W;
};

#endif

